﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Controls;
using System.Threading;

namespace FlyingCell
{
    class Line : UserControl3DGroup
    {

        /// <summary>
        /// Constructor
        /// </summary>
        public Line(string LineName)
        {
            // TODO: not to be here
            mPosition = new Point3D(0, 0, 0);

            mName = LineName;           
            
            // TODO: Change hard coded Cell number
            for (int i = 0; i < 10; i++)
            {
                Cell TmpCell = new Cell(mName + "." + i.ToString());

                TmpCell.mPosition = new Point3D(mPosition.X + i * 125, mPosition.Y, mPosition.Z);

                Add(TmpCell);
            }
        }

        /// <summary>
        /// Show the line as selected
        /// </summary>
        public override void Select()
        {
            // TODO: Selection might depend on visible object and their order, not always on index 0!!!
            base.Select(0); ;
        }

        //-------------------------------------------
        // SCROLLING
        //-------------------------------------------

        /// <summary>
        /// Scroll the complete line to the left (one step)
        /// </summary>
        /// <param name="DurationMs">Duration of the scrolling</param>
        public void StartScrollLeft(int DurationMs)
        {
            // TODO: Here we implement a rolling (not scrolling)!!!!

            Unselect();

            int TmpCount = Count();
            for (ScrollLeftAnimationCounter = 0; ScrollLeftAnimationCounter < TmpCount; ScrollLeftAnimationCounter++)
            {
                Cell TmpCell = (Cell) Get(ScrollLeftAnimationCounter);
                TmpCell.XTranslationCompleted += ScrollLeftTranslationsCompleted;
                // TODO: hardcoded value
                TmpCell.StartXTranslation(-125, DurationMs);
            }
        }

        /// <summary>
        /// ScrollLeft completed event on which external class can register a handler
        /// </summary>
        public event EventHandler<UserControl3DStatusReport> ScrollLeftCompleted;

        /// <summary>
        /// Count down the completed animations
        /// </summary>
        private int ScrollLeftAnimationCounter = 0;

        /// <summary>
        /// Called everytime a Cell animation is finished and terminate the scroll animation properly
        /// </summary>
        /// <param name="sender">The cell</param>
        /// <param name="e">The event arg</param>
        private void ScrollLeftTranslationsCompleted(object sender, UserControl3DStatusReport e)
        {
            // TODO: Protect access to this int
            ScrollLeftAnimationCounter--;

            (sender as Cell).XTranslationCompleted -= ScrollLeftTranslationsCompleted;

            if (ScrollLeftAnimationCounter == 0)
            {
                Cell TmpCell = (Cell)Get(0);
                TmpCell.mPosition = new Point3D(mPosition.X + ((Count() - 1) * 125), mPosition.Y, mPosition.Z);
                Remove(TmpCell);
                Add(TmpCell);

                Select();

                // raise event
                if (ScrollLeftCompleted != null)
                {
                    UserControl3DStatusReport Args = new UserControl3DStatusReport(this);
                    ScrollLeftCompleted(this, Args);
                }

            }
        }

        /// <summary>
        /// Scroll the complete line to the right (one step)
        /// </summary>
        /// <param name="DurationMs">Duration of the scrolling</param>
        public void StartScrollRight(int DurationMs)
        {
            // TODO: Here we implement a rolling (not scrolling)!!!!

            Unselect();

            for (ScrollRightAnimationCounter = 0; ScrollRightAnimationCounter < Count(); ScrollRightAnimationCounter++)
            {
                Cell TmpCell = (Cell)Get(ScrollRightAnimationCounter);
                TmpCell.XTranslationCompleted += ScrollRightTranslationsCompleted;
                // TODO: hardcoded value
                TmpCell.StartXTranslation(-125, DurationMs);
            }
        }

        /// <summary>
        /// ScrollRight completed event on which external class can register a handler
        /// </summary>
        public event EventHandler<UserControl3DStatusReport> ScrollRightCompleted;

        /// <summary>
        /// Count down the completed animations
        /// </summary>
        private int ScrollRightAnimationCounter = 0;

        /// <summary>
        /// Called everytime a Cell animation is finished and terminate the scroll animation properly
        /// </summary>
        /// <param name="sender">The cell</param>
        /// <param name="e">The event arg</param>
        private void ScrollRightTranslationsCompleted(object sender, UserControl3DStatusReport e)
        {
            // TODO: Protect access to this int
            ScrollRightAnimationCounter--;

            (sender as Cell).XTranslationCompleted -= ScrollRightTranslationsCompleted;

            if (ScrollRightAnimationCounter == 0)
            {
                Cell TmpCell = (Cell)Get(Count() - 1);
                TmpCell.mPosition = new Point3D(mPosition.X, mPosition.Y, mPosition.Z);
                Remove(TmpCell);
                Insert(0, TmpCell);

                Select();

                // raise event
                if (ScrollRightCompleted != null)
                {
                    UserControl3DStatusReport Args = new UserControl3DStatusReport(this);
                    ScrollRightCompleted(this, Args);
                }
            }
        }
    }
}
